﻿#region what
/*using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Globalization;
using System.Threading;
using System.Resources;
using MapCenterSystem.Utility;

namespace MobiMap.MasterPage
{
    public partial class MobiMap : System.Web.UI.MasterPage
    {        
        ResourceManager rm;
        CultureInfo ci;
        public string masterString;
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {                
                rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
                
                if (Request.Cookies["language"] != null)
                {
                    string language = Request.Cookies["language"].Value;
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                    LoadString(Thread.CurrentThread.CurrentCulture);

                    idLanguage.Src = "../Image/" + language + ".png";

                    return;
                }
                
                //Defaut language is Vietnamese
                Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
                ci = Thread.CurrentThread.CurrentCulture;
                LoadString(ci); 
            }
            else
            {                
                rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
                ci = Thread.CurrentThread.CurrentCulture;
                SetSession(ci);
            }
        }

        public void LoadString(CultureInfo ci)
        {
            txtAddress.Value = rm.GetString("Address_txtSearch", ci);
            //btnAddressGo.Text = rm.GetString("Search_ButtonText", ci);
            //btnAddressClose.Text = rm.GetString("Close_ButtonText", ci);

            txtStartPoint.Value = rm.GetString("Direction_startPoint", ci);
            txtEndPoint.Value = rm.GetString("Direction_endPoint", ci);
            //btnDirectionGo.Text = rm.GetString("Search_ButtonText", ci);
            //btnDirectionClose.Text = rm.GetString("Close_ButtonText", ci);

            txtService.Value = rm.GetString("Service_txtSearch", ci);
            txtNearBy.Value = rm.GetString("Service_txtNearBy", ci);
            //btnServiceGo.Text = rm.GetString("Search_ButtonText", ci);
            //btnServiceClose.Text = rm.GetString("Close_ButtonText", ci);

            SetSession(ci);
        }

        public void SetSession(CultureInfo ci)
        {
            Session["address"] = rm.GetString("Address_txtSearch", ci);
            Session["startpoint"] = rm.GetString("Direction_startPoint", ci);
            Session["endpoint"] = rm.GetString("Direction_endPoint", ci);
            Session["service"] = rm.GetString("Service_txtSearch", ci);
            Session["nearby"] = rm.GetString("Service_txtNearBy", ci);
            //MY LOCATION-------------------------------------------------------------------------------------
            //Session["myLocation"] = rm.GetString("Se
        }

        protected void idHome_Click(object sender, ImageClickEventArgs e)
        {
            Session.Remove("addr");
            Session.Remove("MAP");
            Session.Remove("mobimap_homeinfo");
            Session.Remove("mobimap_lstGraphNode");
            Response.Redirect("../Guest/Home.aspx");
        }

        public void btnAddressGo_Click(Object sender, EventArgs e)
        {
            Session.Remove("mobimap_direction");
            Session.Remove("mobimap_lstGraphNode");

            string strAddress = txtAddress.Value;
            strAddress = StringHelper.StandardizeString(strAddress);
            if(strAddress != string.Empty)
                Response.Redirect("ResultAddress.aspx?addr=" + strAddress);
        }        

        protected void btnServiceGo_Click(object sender, EventArgs e)
        {
            string service = txtService.Value;
            string nearby = txtNearBy.Value;
            service = StringHelper.StandardizeString(service);
            nearby = StringHelper.StandardizeString(nearby);
            if (service == string.Empty || nearby == string.Empty)
                return;
            
            string[] s = { service, nearby };
            Session["mobimap_service"] = s;

            String url = "ResultNearBy.aspx";
            if (hiddenLocation.Value != "")
            {
                string[] strInfoAndFlag = hiddenLocation.Value.Split('_');

                if (strInfoAndFlag.Length == 2)
                {
                    if (strInfoAndFlag[1] == "address")
                    {
                        Session["mobimap_nearbyinfo"] = Session["mobimap_homeinfo"];
                    }
                    else
                    {
                        if (strInfoAndFlag[1] == "start")
                            Session["mobimap_nearbyinfo"] = Session["mobimap_startinfo"];
                        else
                            Session["mobimap_nearbyinfo"] = Session["mobimap_endinfo"];
                    }

                    if(Session["mobimap_nearbyinfo"] != null)
                        url = "ResultService.aspx";
                }
            }
            
            Session.Remove("mobimap_homeinfo");
            Session.Remove("mobimap_lstGraphNode");            

            Response.Redirect(url);
        }

        protected void btnDirectionGo_Click(object sender, EventArgs e)
        {           
            //Session.Remove("mobimap_homeinfo");

            string start = txtStartPoint.Value;
            string end = txtEndPoint.Value;
            start = StringHelper.StandardizeString(start);
            end = StringHelper.StandardizeString(end);
            if (start == string.Empty || end == string.Empty)
                return;

            string[] s = { start, end };
            Session["mobimap_direction"] = s;

            String url = "ResultStartPoint.aspx";

            
            if (hiddenLocation.Value != "none")
            {
                string[] strInfoAndFlag = hiddenLocation.Value.Split('_');
                string info = strInfoAndFlag[0];
                string flag = strInfoAndFlag[1];
                
                if (flag == "start")
                {                    
                    if (info == "end")
                        Session["mobimap_startinfo"] = Session["mobimap_endinfo"];
                    else if(info == "address")
                        Session["mobimap_startinfo"] = Session["mobimap_homeinfo"];

                    if(Session["mobimap_startinfo"] != null)
                        url = "ResultEndPoint.aspx";

                    Session.Remove("mobimap_endinfo");
                    Session.Remove("mobimap_homeinfo");
                }
                else
                {
                    if(info == "start")
                        Session["mobimap_endinfo"] = Session["mobimap_startinfo"];
                    else if(info == "address")
                        Session["mobimap_endinfo"] = Session["mobimap_homeinfo"];

                    Session.Remove("mobimap_startinfo");
                    Session.Remove("mobimap_homeinfo");
                }
            }
            else
            {
                Session.Remove("mobimap_endinfo");
                Session.Remove("mobimap_startinfo");
                Session.Remove("mobimap_homeinfo");
            }

            Response.Redirect(url);
        }

        protected void btnMyLocation_Click(object sender, EventArgs e)
        {
            string[] homeInfo = { "227 Nguyễn Văn Cừ P.4 Q5 TP.HCM", " ", "106.682568", "10.762795", "myLocation" };
            Session["mobimap_homeinfo"] = homeInfo;

            Response.Redirect("Home.aspx");
        }

    }
}

*/
#endregion
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Globalization;
using System.Threading;
using System.Resources;
using MapCenterSystem.Utility;
using System.Collections.Generic;
using MapCenterSystem.DataInfo;
using MapCenterSytem.Process;
using MapCenterSystem.DefineType;

namespace MobiMap.MasterPage
{
    public partial class MobiMap : System.Web.UI.MasterPage
    {        
        ResourceManager rm;
        CultureInfo ci;
        public string masterString;
        public string longtatude;
        public string lattatude;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                if (Request.Cookies["mobimap_scrWidth"] != null)
                {
                    int scrWidth = int.Parse(Request.Cookies["mobimap_scrWidth"].Value);
                    if (scrWidth > 512)
                    {
                        int leftPosition = (scrWidth - 512) / 2;
                        HtmlControl divToolbarWrapper = (HtmlControl)Master.FindControl("divToolbarWrapper");
                        divToolbarWrapper.Style["left"] = leftPosition.ToString() + "px";
                    }
                }

                rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
                
                if (Request.Cookies["language"] != null)
                {
                    string language = Request.Cookies["language"].Value;
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                    LoadString(Thread.CurrentThread.CurrentCulture);

                    idLanguage.Src = "../Image/" + language + ".png";

                    return;
                }
                
                //Defaut language is Vietnamese
                Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
                ci = Thread.CurrentThread.CurrentCulture;
                LoadString(ci);
                
            }
            else
            {                
                rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
                ci = Thread.CurrentThread.CurrentCulture;
                SetSession(ci);
            }
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "GetLongAndLatHTML5();", true);           
        }

        public void LoadString(CultureInfo ci)
        {
            txtAddress.Value = rm.GetString("Address_txtSearch", ci);
            //btnAddressGo.Text = rm.GetString("Search_ButtonText", ci);
            //btnAddressClose.Text = rm.GetString("Close_ButtonText", ci);

            txtStartPoint.Value = rm.GetString("Direction_startPoint", ci);
            txtEndPoint.Value = rm.GetString("Direction_endPoint", ci);
            //btnDirectionGo.Text = rm.GetString("Search_ButtonText", ci);
            //btnDirectionClose.Text = rm.GetString("Close_ButtonText", ci);

            txtService.Value = rm.GetString("Service_txtSearch", ci);
            txtNearBy.Value = rm.GetString("Service_txtNearBy", ci);
            //btnServiceGo.Text = rm.GetString("Search_ButtonText", ci);
            //btnServiceClose.Text = rm.GetString("Close_ButtonText", ci);

            SetSession(ci);
        }

        public void SetSession(CultureInfo ci)
        {
            Session["address"] = rm.GetString("Address_txtSearch", ci);
            Session["startpoint"] = rm.GetString("Direction_startPoint", ci);
            Session["endpoint"] = rm.GetString("Direction_endPoint", ci);
            Session["service"] = rm.GetString("Service_txtSearch", ci);
            Session["nearby"] = rm.GetString("Service_txtNearBy", ci);
            //MY LOCATION-------------------------------------------------------------------------------------
            //Session["myLocation"] = rm.GetString("Se
        }

        protected void idHome_Click(object sender, ImageClickEventArgs e)
        {
            Session.Remove("addr");
            Session.Remove("MAP");
            Session.Remove("mobimap_homeinfo");
            Session.Remove("mobimap_lstGraphNode");
            Session.Remove("mobimap_lstService");
            Session.Remove("mobimap_mylocation");
            Response.Redirect("../Guest/Home.aspx");
        }

        public void btnAddressGo_Click(Object sender, EventArgs e)
        {
            Session.Remove("mobimap_lstGraphNode");
            Session.Remove("mobimap_lstService");
            Session.Remove("mobimap_mylocation");

            string strAddress = txtAddress.Value;
            strAddress = StringHelper.StandardizeString(strAddress);
            if(strAddress != string.Empty)
                Response.Redirect("ResultAddress.aspx?addr=" + strAddress);
        }        

        protected void btnServiceGo_Click(object sender, EventArgs e)
        {
            string service = txtService.Value;
            string nearby = txtNearBy.Value;
            service = StringHelper.StandardizeString(service);
            nearby = StringHelper.StandardizeString(nearby);
            if (service == string.Empty || nearby == string.Empty)
                return;
            
            string[] s = { service, nearby };
            Session["mobimap_service"] = s;

            String url = "ResultNearBy.aspx";
            if (hiddenLocation.Value != "")
            {               
                string[] strInfoAndFlag = hiddenLocation.Value.Split('_');

                if (strInfoAndFlag.Length == 2)
                {
                    if (strInfoAndFlag[1] == "address")
                    {
                        if (Session["mobimap_homeinfo"] != null)
                        {
                            Session["mobimap_nearbyinfo"] = Session["mobimap_homeinfo"];
                        }
                        else
                        {
                            Session["mobimap_nearbyinfo"] = Session["mobimap_mylocation"];
                        }
                    }
                    else
                    {
                        if (strInfoAndFlag[1] == "start")
                            Session["mobimap_nearbyinfo"] = Session["mobimap_startinfo"];
                        else
                            Session["mobimap_nearbyinfo"] = Session["mobimap_endinfo"];
                    }

                    if (Session["mobimap_nearbyinfo"] != null)
                        url = "ResultService.aspx";                   
                }
                else
                {
                    Session.Remove("mobimap_lstService");
                    Session.Remove("mobimap_mylocation");
                }
            }            
            
            Session.Remove("mobimap_homeinfo");
            Session.Remove("mobimap_lstGraphNode");
            

            Response.Redirect(url);
        }

        protected void btnDirectionGo_Click(object sender, EventArgs e)
        {
            string start = txtStartPoint.Value;
            string end = txtEndPoint.Value;
            start = StringHelper.StandardizeString(start);
            end = StringHelper.StandardizeString(end);
            if (start == string.Empty || end == string.Empty)
                return;

            string[] s = { start, end };
            Session["mobimap_direction"] = s;

            String url = "ResultStartPoint.aspx";

            
            if (hiddenLocation.Value != "none")
            {
                string[] strInfoAndFlag = hiddenLocation.Value.Split('_');
                string info = strInfoAndFlag[0];
                string flag = strInfoAndFlag[1];
                
                if (flag == "start")
                {                    
                    if (info == "end")
                        Session["mobimap_startinfo"] = Session["mobimap_endinfo"];
                    else if(info == "address")
                        Session["mobimap_startinfo"] = Session["mobimap_homeinfo"];

                    if(Session["mobimap_startinfo"] != null)
                        url = "ResultEndPoint.aspx";

                    Session.Remove("mobimap_endinfo");
                    Session.Remove("mobimap_homeinfo");
                }
                else
                {
                    if(info == "start")
                        Session["mobimap_endinfo"] = Session["mobimap_startinfo"];
                    else if(info == "address")
                        Session["mobimap_endinfo"] = Session["mobimap_homeinfo"];

                    Session.Remove("mobimap_startinfo");
                    Session.Remove("mobimap_homeinfo");
                }
            }
            else
            {
                Session.Remove("mobimap_endinfo");
                Session.Remove("mobimap_startinfo");
                Session.Remove("mobimap_homeinfo");
                Session.Remove("mobimap_mylocation");
                Session.Remove("mobimap_lstGraphNode");
                Session.Remove("mobimap_lstService");
            }

            Response.Redirect(url);
        }
        //Sự kiện thay đổi hiddenfield
        protected void ValueHiddenField_ValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hdlat1.Value) && !string.IsNullOrEmpty(hdlong1.Value))
            {
                String[] myloc = { hdlat1.Value, hdlong1.Value };
                Session["myloc"] = myloc;
            }
            else
            {
                Session["myloc"] = null;
            }            
        }
        // Su kien Vi tri cua toi
        protected void btnMyLocation_Click(object sender, EventArgs e)
        {
            
            //thêm chỗ này để lấy vị trí tọa độ trả về
            
            string myLongitude = hdlong1.Value;
            string myLatitude = hdlat1.Value;

            if (Session["myloc"] != null)
            {
                string[] myloc = (string[])Session["myloc"];
                myLatitude = myloc[0];
                myLongitude = myloc[1];
            }
            else
            {
                myLongitude = hdlong1.Value;
                myLatitude = hdlat1.Value;
            }
            if (String.IsNullOrEmpty(myLongitude) || String.IsNullOrEmpty(myLatitude))
            {
                myLongitude = "106.682568";
                myLatitude = "10.762795";
                //return;
            }
            //myLongitude = "106.682568";
            //myLatitude = "10.762795";
            double dlongtatude = double.Parse(myLongitude, CultureInfo.InvariantCulture);
            double dlatitude = double.Parse(myLatitude, CultureInfo.InvariantCulture);            
            //cách này oke chạy hơi chậm 
            MyLocation diadiem = MyLocationBUS.layChiTietTenDuongViTriHienTai(dlatitude, dlongtatude);
            string result = diadiem.Number + " " + diadiem.Area;
            result = result.Substring(0, result.IndexOf('.') - 1);
            result = StringHelper.StandardizeString(result);
            if (String.IsNullOrEmpty(result))
            {
                result = "227 Nguyễn Văn Cừ";
            }
            string[] myLocation = { result, " ", myLongitude, myLatitude, "myLocation" };
            MyLocation lClass = new MyLocation();
            Session["mobimap_mylocation"] = myLocation;
            Session.Remove("mobimap_lstService");
            Session.Remove("mobimap_homeinfo");
            Session.Remove("mobimap_lstGraphNode");
            Response.Redirect("Home.aspx");
        }
    }
} 
 